package cn.bytecollege.admin.controller; /**
 * @Auther：MR.P
 * @Date:2021/10/28 10 53
 * @Description:添加分类
 */

import cn.bytecollege.admin.dao.ClassifyDao;
import cn.bytecollege.admin.service.ClassifyService;
import cn.bytecollege.common.entity.Classify;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "ClassifySaveController", value = "/admin/classifySave")
public class ClassifySaveController extends HttpServlet {
    private ClassifyService classifyService = null;
    public ClassifySaveController(){
        classifyService = new ClassifyService();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/admin/html/device/classifySave.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String msg = null;
        String classifyName = request.getParameter("classifyName");
        Classify classify = new Classify();

        classify.setClassifyName(classifyName);
        //判断tempClassify是否为空，不为空直接返回
        Classify tempClassify = classifyService.findByClassifyName(classifyName);
        if(tempClassify != null){
            msg = "该分类名字已存在";
            request.setAttribute("msg",msg);
            request.getRequestDispatcher("/admin/html/device/classifySave.jsp").forward(request,response);
            return;
        }
        //tempClassify不为空，直接返回，以下代码不执行
        classifyService.save(classify);
        msg = "添加成功";
        request.setAttribute("msg",msg);
        request.getRequestDispatcher("/admin/html/device/classifySave.jsp").forward(request,response);
    }
}
